<?php 
require('connection.php');
require_once('inc/constants.inc.php');
// error_reporting (E_ALL ^ E_NOTICE);
// photo gallery settings
$mainFolder    = 'albums';   // folder where your albums are located - relative to root
$albumsPerPage = '12';       // number of albums per page
$itemsPerPage  = '12';       // number of images per page    
$thumb_width   = '150';      // width of thumbnails
//$thumb_height  = '85';       // height of thumbnails
$extensions    = array(".jpg",".png",".gif",".JPG",".PNG",".GIF"); // allowed extensions in photo gallery

// Get the access token with app_id and app_secret
$urlToGetAccessToken = 'https://graph.facebook.com/oauth/access_token';
$dataToGetAccessToken = array('client_id' => APP_ID,
	'client_secret'=> APP_SECRET,
	'grant_type'=>'client_credentials'
);
$accessTokenResult = explode('=',curlUsingGetWithData($urlToGetAccessToken, $dataToGetAccessToken));

$access_token = $accessTokenResult['1'];


if (!isset($_GET['album'])) {

// Get the albums
$urlToRetrieveAlbum = 'https://graph.facebook.com/MyArunachal';


//$urlToRetrieveAlbum = 'https://graph.facebook.com/SmartekConsultancy';
$dataToRetrieveAlbum = array(
'fields'=>'albums.limit(100)',
'access_token'=>$access_token
);
    // display list of albums

	// retrieve data
	$albumResult = curlUsingGetWithData($urlToRetrieveAlbum, $dataToRetrieveAlbum);
	$albumResult = json_decode($albumResult);
	
	$albums = $albumResult->albums->data;
	
	//print_r($albums);
	$album_count = count($albums)-1;
	echo '<div class="titlebar">
		<div class="float-left"><span class="title">Photo Gallery</span> - Albums</div>
		<div class="float-right">'.$album_count.' albums</div>
	 </div>';
	foreach($albums as $album)
	{
		$albumid = $album->id;
		if(isset($album->name))
		{
			$album_title = $album->name;
		}
		if(isset($album->cover_photo))
		$album_cover_id = $album->cover_photo;
		else
		$album_cover_id = '';
		if(isset($album->count))
		$photo_count = $album->count;
		else
		$photo_count = '';
		
		$urlToRetrieveAlbumCover = "https://graph.facebook.com/".$album_cover_id;
		//echo $urlToRetrieveAlbumCover;
		$album_cover_results = curlUsingGet($urlToRetrieveAlbumCover);
		$album_cover_results = json_decode($album_cover_results);
		echo "<pre>";
		//print_r($album_cover_results);
		echo "</pre>";
		if(isset($album_cover_results->picture))
			$album_cover_url = $album_cover_results->picture;
		else
			$album_cover_url = '';
	
			if(!empty($album_cover_url))
				echo '<div class="thumb-album shadow">
				
				<div class="thumb-wrapper">
				   <a href="'.$_SERVER['PHP_SELF'].'?album='.urlencode($albumid).'&title='.urlencode($album_title).'">
					 <img src="'. $album_cover_url .'" width="'.$thumb_width.'" alt="" />
				   </a>	
				</div>
				
				<div class="p5"></div>
				
				<a href="'.$_SERVER['PHP_SELF'].'?album='. urlencode($albumid) .'">
				<span class="caption">'. $album_title .'</span>
				</a>
			
			  </div>';

	} 
} else {

	// Retrieve photos from album
	$album_id = $_GET['album'];
	$urlToRetrievePhotos = "https://graph.facebook.com/".$album_id."/photos";
	
	$photosResult = curlUsingGet($urlToRetrievePhotos);
	$photosResult = json_decode($photosResult);
	$photos = $photosResult->data;

	$photo_count = count($photos);
	$album_title = urldecode($_GET['title']);
   echo '<div class="titlebar">
           <div class="float-left"><span class="title">'. $album_title .'</span> - <a href="'.$_SERVER['PHP_SELF'].'">View All Albums</a></div>
           <div class="float-right">'.$photo_count.' images</div>
         </div>';	  
   echo '<div class="clear"></div>';
	foreach($photos as $photo)
	{
		$photo_url = $photo->images[4]->source;
		//echo $photo->images[0]->source;
		$cover_photo_url = $photo->picture;
		 
	        echo '<div class="thumb shadow">
	                <div class="thumb-wrapper">
					<a href="'.$photo_url .'" class="albumpix" rel="albumpix">
				      <img src="'.$cover_photo_url .'" width="'.$thumb_width.'" alt="" />
				    </a>
					</div>  
			      </div>'; 

	}
 
}


// create thumbnails from images
function make_thumb($folder,$src,$dest,$thumb_width) {

	$source_image = imagecreatefromjpeg($folder.'/'.$src);
	$width = imagesx($source_image);
	$height = imagesy($source_image);
	
	$thumb_height = floor($height*($thumb_width/$width));
	
	$virtual_image = imagecreatetruecolor($thumb_width,$thumb_height);
	
	imagecopyresampled($virtual_image,$source_image,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
	
	imagejpeg($virtual_image,$dest,100);
	
}

// display pagination
function print_pagination($numPages,$urlVars,$currentPage) {
        
   if ($numPages > 1) {
      
	   echo 'Page '. $currentPage .' of '. $numPages;
	   echo '&nbsp;&nbsp;&nbsp;';
   
       if ($currentPage > 1) {
	       $prevPage = $currentPage - 1;
	       echo '<a href="?'. $urlVars .'p='. $prevPage.'">&laquo;&laquo;</a> ';
	   }	   
	   
	   for( $e=0; $e < $numPages; $e++ ) {
           $p = $e + 1;
       
	       if ($p == $currentPage) {	    
		       $class = 'current-paginate';
	       } else {
	           $class = 'paginate';
	       } 
	       

		       echo '<a class="'. $class .'" href="?'. $urlVars .'p='. $p .'">'. $p .'</a>';
		  	  
	   }
	   
	   if ($currentPage != $numPages) {
           $nextPage = $currentPage + 1;	
		   echo ' <a href="?'. $urlVars .'p='. $nextPage.'">&raquo;&raquo;</a>';
	   }	  	 
   
   }

}
?>